﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace PL_TileEditor
{
    public partial class TileViewer : ScrollableControl
    {
        public Bitmap TileImage = null;
        public Size TileSize = new Size(64, 64);
        public int SelectedTile = 0;
        public List<Tile> TileProperties = new List<Tile>();
        public String TileImageFile = "";

        public TileViewer()
        {
            TileImage = PL_TileEditor.Properties.Resources.Tiles01;
            AutoScrollMinSize = new Size(TileImage.Width, TileImage.Height);
            DoubleBuffered = true;
            ChangeTileSize(64, 64);
            InitializeComponent();
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            if (TileImage != null) {
                e.Graphics.DrawImage(TileImage, new Rectangle(AutoScrollPosition.X, AutoScrollPosition.Y, TileImage.Width, TileImage.Height));

                for (int x = 0; x < TileImage.Width / TileSize.Width; x++)
                {
                    for (int y = 0; y < TileImage.Height / TileSize.Height; y++)
                    {
                        e.Graphics.DrawRectangle(Pens.Black, new Rectangle(x * TileSize.Width + AutoScrollPosition.X, y * TileSize.Height + AutoScrollPosition.Y, TileSize.Width, TileSize.Height));
                    }
                }

                e.Graphics.DrawRectangle(Pens.Red, new Rectangle(new Point((SelectedTile % (TileImage.Size.Width / TileSize.Width))*TileSize.Width + AutoScrollPosition.X, (SelectedTile / (TileImage.Size.Width / TileSize.Width)) * TileSize.Height + AutoScrollPosition.Y), TileSize));

            }
            
            base.OnPaint(e);
        }

        protected override void OnMouseDown(MouseEventArgs e)
        {
            Focus();
            int OldSelectedTile = SelectedTile;
            if (e.Button == MouseButtons.Left)
            {
                if (e.X > 0 && e.X < (TileImage.Width / TileSize.Width) * TileSize.Width && e.Y > 0 && e.Y < (TileImage.Height / TileSize.Height) * TileSize.Height)
                    SelectedTile = ((e.X - AutoScrollPosition.X) / TileSize.Width) + (((e.Y - AutoScrollPosition.Y) / TileSize.Height) * (TileImage.Width / TileSize.Width));
            }

            if (OldSelectedTile != SelectedTile)
            {
                Invalidate();
            }

            base.OnMouseDown(e);
        }

        protected override void OnMouseMove(MouseEventArgs e)
        {
            int OldSelectedTile = SelectedTile;
            if (e.Button == MouseButtons.Left)
            {
                if (e.X > 0 && e.X < (TileImage.Width / TileSize.Width) * TileSize.Width && e.Y > 0 && e.Y < (TileImage.Height / TileSize.Height) * TileSize.Height)
                SelectedTile = ((e.X-AutoScrollPosition.X) / TileSize.Width) + (((e.Y-AutoScrollPosition.Y) / TileSize.Height) * (TileImage.Width / TileSize.Width));
            }

            if (OldSelectedTile != SelectedTile)
            {
                Invalidate();
            }

            base.OnMouseMove(e);
        }

        public void ChangeImage(String NewImage)
        {
            TileImageFile = NewImage;
            Bitmap lulz = (Bitmap)(Bitmap.FromFile(NewImage));
            TileImage = lulz;
            AutoScrollMinSize = new Size(TileImage.Width, TileImage.Height);
            TileProperties.Clear();
            for (int i = 0; i < (TileImage.Width / TileSize.Width) * (TileImage.Height / TileSize.Height); i++)
            {
                TileProperties.Add(new Tile());
            }
            Invalidate();
        }

        public void ChangeTileSize(int Width, int Height)
        {
            TileSize = new Size(Width, Height);
            TileProperties.Clear();
            for (int i = 0; i < (TileImage.Width / TileSize.Width) * (TileImage.Height / TileSize.Height); i++)
            {
                TileProperties.Add(new Tile());
            }
        }
    }
}
